package com.kunpan.admin.aop;

import cn.dev33.satoken.stp.StpUtil;
import com.kunpan.admin.annotation.AutoFill;
import com.kunpan.admin.annotation.FileOperate;
import com.kunpan.admin.common.Enum.BusinessType;
import com.kunpan.admin.common.domain.AjaxResult;
import com.kunpan.admin.common.redis.RedisCache;
import com.kunpan.admin.common.redis.RedisMessagePublisher;
import com.kunpan.admin.constant.Constants;
import com.kunpan.admin.constant.HttpStatus;
import com.kunpan.admin.entity.PanFile;
import com.kunpan.admin.entity.dto.MessageDto;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class FileOperateAspect {
    private static final Logger log = LoggerFactory.getLogger(AutoFillAspect.class);

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private RedisMessagePublisher redisMessagePublisher;


    /**
     * 定义切点,表示具体执行的点，也就是什么时候执行通知的点
     */
    @Pointcut("execution(* com.kunpan.*.controller.FileOperateController.*(..)) && @annotation(com.kunpan.admin.annotation.FileOperate)")
    public void autoFilePointCut() {
    }

    @AfterReturning(value = "autoFilePointCut()", returning = "result")
    public void beforeSet(JoinPoint joinPoint, Object result) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        // 获取方法上的 @AutoFill 注解
        FileOperate FileOperate = method.getAnnotation(FileOperate.class);
        //2.1 获取参数
        Object[] args = joinPoint.getArgs();
        // 获取操作类型
        BusinessType businessType = FileOperate.value();
        Integer fileType = null;
        Integer userId = Integer.valueOf(StpUtil.getLoginId().toString());
        //方法返回值要为200，才进行清除缓存操作
        if (((AjaxResult) result).get(AjaxResult.CODE_TAG).equals(HttpStatus.SUCCESS)) {
            //操作为还原文件
            if (BusinessType.RECOVERFILE.equals(businessType)) {
                String deleteKey = String.format(Constants.USER_FILE_CACHE_KEY, userId);
                redisCache.deleteKeysByPattern(deleteKey);
                return;
            } else if (BusinessType.DELETEFILE.equals(businessType)) {
                fileType = (Integer) args[1];
            } else if (BusinessType.MOVEFILE.equals(businessType)) {//操作为移动文件位置
                fileType = (Integer) args[2];
            } else if (BusinessType.RENAMEFILE.equals(businessType)) {
                PanFile panFile = (PanFile) args[0];
                fileType = panFile.getFileType();
            }
            //发布清除对应缓存的消息
            MessageDto messageDto = MessageDto.create(userId, fileType, 0L, BusinessType.OTHER.toString());
            redisMessagePublisher.publishFileUploadEvent(messageDto);
        }
    }
}
